package com.wlcorp.forcazamis.sys.util;


/**
 * 
 * ***********************************************
 * 
 * @file: LoginSecurity.java
 * @Copyright: 2007 HundSun Electronics Co.,Ltd. All right reserved.
 * ***********************************************
 * @package: com.hs.cccecity.sys.utils
 * @class: LoginSecurity
 * @description:
 * 
 * @author: huangjb
 * @since: May 16, 2007-5:33:46 PM
 * @history: $Id: LoginSecurity.java,v 1.1 2007/08/09 00:46:22 lilz Exp $
 */
public class LoginSecurity {
	
	private static final byte[] ukey = "handsome".getBytes();

	public LoginSecurity() {
		
	}

	/**
	 * �ַ����
	 * 字符串加密
	 * @param sourcestring
	 * @return
	 */
	public static String encryptStr(String source){
//		if (sourcestring.length() < 8)
//			throw new Exception("�����ַ��Ȳ�������8λ");
//		Encryptor enc = new Encryptor(ukey);
//		byte[] endata = enc.encrypt(sourcestring.getBytes());
//		String rtnVal = base64Encode(endata);
//		return rtnVal;
		if (source == null || "".equals(source)) {
	      return "";
	    }
	    String rtn = "";
	    byte c;
	    for (int i = 0; i < source.length(); i++) {
	      c = (byte) source.charAt(i);
	      rtn += (char) ( ( (byte) (c - (byte) 32)) ^ 0x7f);
	    }
	    return rtn;
	}

	/**
	 * �ַ����
	 * 字符串解密
	 * @param sourcestring
	 * @return
	 */
	public static String decryptStr(String source){
//		Encryptor enc = new Encryptor(ukey);
//		sourcestring = sourcestring.replaceAll(" ", "+");
//		byte[] data = base64DecodeToBytes(sourcestring);
//		byte[] dedata = enc.decrypt(data);
//		String rtnVal = new String(dedata);
//		return rtnVal.trim();
		if (source == null || "".equals(source)) {
	      return "";
	    }
	    String rtn = "";
	    char c;
	    for (int i = 0; i < source.length(); i++) {
	      c = source.charAt(i);
	      rtn += (char) ( (c ^ 0x7f) + 32);
	    }
	    return rtn;
	}

	/**
	 * DES字符串加密
	 * @param src
	 * @return
	 */
	public static String desEncryptStr(String src) {
		if (src.length() < 8) {
			src = src + "        ";
		}
		Encryptor enc = new Encryptor(ukey);
		byte[] endata = enc.encrypt(src.getBytes());
		String rtnVal = base64Encode(endata);
		return rtnVal;
	}

	/**
	 * des解密字符串
	 * @param crypt
	 * @return
	 */
	public static String desDecryptStr(String crypt) {
		Encryptor enc = new Encryptor(ukey);
		crypt = crypt.replaceAll(" ", "+");
		byte[] data = base64DecodeToBytes(crypt);
		byte[] dedata = enc.decrypt(data);
		String rtnVal = new String(dedata);
		return rtnVal.trim();
	}


	/**
	 * BASE64 编码
	 * @param src
	 * @return
	 */
	public static String base64Encode(String src) {
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

		return encoder.encode(src.getBytes());
	}

	/**
	 * BASE64 编码(byte[]).
	 * @param src
	 * @return
	 */
	public static String base64Encode(byte[] src) {
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		return encoder.encode(src);
	}

	/**
	 * BASE64 解码
	 * @param src
	 * @return
	 */
	public static String base64Decode(String src) {
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		try {
			return new String(decoder.decodeBuffer(src));
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * BASE64 解码(to byte[])
	 * @param src
	 * @return
	 */
	public static byte[] base64DecodeToBytes(String src) {
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		try {
			return decoder.decodeBuffer(src);
		} catch (Exception ex) {
			return null;
		}
	}
	
	public static void main(String[] args){
			System.out.println(LoginSecurity.encryptStr("888888"));

	}
}
